//
//  JSSTextView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/26.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit

class JSSTextView: UITextView {
    
    var placeHolderLable:String? {
        
        didSet {
            //从外界传值进来 把 lable的文字改掉
            self.lable.text = placeHolderLable
        }
    }
    
    //重写lable的font属性 通过从外界传入的字体大小控制lable的字体大小
   override var font: UIFont? {
    
    didSet {
        self.lable.font = font
        
    }
    
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
//        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        backgroundColor = UIColor.whiteColor()
        
        //添加lable
        addSubview(lable)
        //约束
        lable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(10)
            make.leading.equalTo(self).offset(6)
        }
        
        //用通知监听textView文字改变 然后隐藏lable(监听textfield的值开始改变的时候调用)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "valueDidChaged", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
// MARK - 实现接收到通知的方法
    @objc private func valueDidChaged(){
        
        print("通知发送成功")
        //隐藏lable
        lable.hidden = hasText()
        //让发送按钮变成正常状态
    }
    
    //移除通知
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private lazy var lable:UILabel = {
    
        let lab = UILabel(textColor: UIColor.lightGrayColor(), textFont: 14)
        
        lab.text = "..哈哈~~~啦啦啦"
        
        lab.sizeToFit()
        
        return lab
    
    }()

}
